//
//  KZTurmpetView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/10/19.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZTurmpetView.h"

@implementation KZTurmpetView
{
    CAShapeLayer *_shapeLayer;
    
    int _tag;
    NSTimer *_timer;
    
    BOOL _isPause;

}

- (instancetype)init {
    if (self = [super init]) {
        
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    UIImageView *turmpetView = [UIImageView new];
    turmpetView.image = [UIImage imageNamed:@"kz-喇叭"];
    [self addSubview:turmpetView];
    [turmpetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(@0);
        make.width.height.equalTo(self.mas_height).multipliedBy(0.6);
        make.centerX.equalTo(self.mas_centerX).offset(-2);
    }];
    
    _shapeLayer = [[CAShapeLayer alloc] init];
    _shapeLayer.fillColor = UIColor.clearColor.CGColor;
    _shapeLayer.strokeColor = UIColor.whiteColor.CGColor;
    _shapeLayer.lineWidth = W(2);
    _shapeLayer.lineCap = kCALineCapRound;
    [self.layer addSublayer:_shapeLayer];
    
    
    // 播放
    [self play];
}

- (instancetype)initWithTurmpetImage:(UIImage *)image Color:(UIColor *)color{
    if (self = [super init]) {
        
        UIImageView *turmpetView = [UIImageView new];
        turmpetView.image = image;//[UIImage imageNamed:@"kz-喇叭"];
        [self addSubview:turmpetView];
        [turmpetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(@0);
            make.width.height.equalTo(self.mas_height).multipliedBy(0.6);
            make.centerX.equalTo(self.mas_centerX).offset(-2);
        }];
    
        _shapeLayer = [[CAShapeLayer alloc] init];
        _shapeLayer.fillColor = UIColor.clearColor.CGColor;
        _shapeLayer.strokeColor = color.CGColor;
        _shapeLayer.lineWidth = W(1.5);
        _shapeLayer.lineCap = kCALineCapRound;
        [self.layer addSublayer:_shapeLayer];
        
    }
    
    return self;
}

- (void)setPath {
    _shapeLayer.path = [self path3].CGPath;
}

- (void)timerAction {
    
    _tag ++;
    
    NSArray<UIBezierPath *> *paths = [self paths];
    
    if (_tag >= paths.count) {
        _tag = 0;
    }
    
    _shapeLayer.path = paths[_tag].CGPath;
}


- (void)play {
    _tag = 0;
    
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(timerAction) userInfo:nil repeats:true];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
- (void)stop {
    _shapeLayer.path = [UIBezierPath bezierPath].CGPath;
    [_timer invalidate];
    _timer = nil;
}
- (void)pause {
    
    _isPause = true;
    
    NSArray<UIBezierPath *> *paths = [self paths];
    _shapeLayer.path = paths[_tag].CGPath;
   
    [_timer setFireDate:[NSDate distantFuture]];
    
}
- (void)resume {
    
    _isPause = false;
    [_timer setFireDate:[NSDate date]];
}
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [super touchesBegan:touches withEvent:event];
//    if (_isPause) {
//        [self resume];
//    } else {
//        [self pause];
//    }
//
//}

- (NSArray *)paths {
    NSArray<UIBezierPath *> *paths = @[[UIBezierPath bezierPath] ,[self path1], [self path2], [self path3]];
    return paths;
}

- (UIBezierPath *)path1 {
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGFloat angle = M_PI / 6;
    CGFloat radius = self.height / 6;
    CGFloat centerX = self.width / 2;
    [path moveToPoint:CGPointMake(centerX + cos(angle) * radius, self.height / 2 - sin(angle) * radius)];
    [path addArcWithCenter:CGPointMake(centerX, self.height / 2) radius:radius startAngle:-angle endAngle:angle clockwise:true];
    return path;
}
- (UIBezierPath *)path2 {
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGFloat angle = M_PI / 6;
    CGFloat radius = self.height / 6;
    CGFloat centerX = self.width / 2;
    [path moveToPoint:CGPointMake(centerX + cos(angle) * radius, self.height / 2 - sin(angle) * radius)];
    [path addArcWithCenter:CGPointMake(centerX, self.height / 2) radius:radius startAngle:-angle endAngle:angle clockwise:true];
    
    radius = self.height / 4;
    [path moveToPoint:CGPointMake(centerX + cos(angle) * radius, self.height / 2 - sin(angle) * radius)];
    [path addArcWithCenter:CGPointMake(centerX, self.height / 2) radius:radius startAngle:-angle endAngle:angle clockwise:true];

    return path;
}
- (UIBezierPath *)path3 {
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGFloat angle = M_PI / 6;
    CGFloat radius = self.height / 6;
    CGFloat centerX = self.width / 2;
    [path moveToPoint:CGPointMake(centerX + cos(angle) * radius, self.height / 2 - sin(angle) * radius)];
    [path addArcWithCenter:CGPointMake(centerX, self.height / 2) radius:radius startAngle:-angle endAngle:angle clockwise:true];
    
    radius = self.height / 4;
    [path moveToPoint:CGPointMake(centerX + cos(angle) * radius, self.height / 2 - sin(angle) * radius)];
    [path addArcWithCenter:CGPointMake(centerX, self.height / 2) radius:radius startAngle:-angle endAngle:angle clockwise:true];
    
    radius = self.height / 3;
    [path moveToPoint:CGPointMake(centerX + cos(angle) * radius, self.height / 2 - sin(angle) * radius)];
    [path addArcWithCenter:CGPointMake(centerX, self.height / 2) radius:radius startAngle:-angle endAngle:angle clockwise:true];

    return path;
}



@end
